const logins = res => {
  // debugger
  var url = getApp().globalData.url
  wx.login({
    complete: (res) => {
      // console.log(res.code)
      // debugger
      url = url + "/oa-applet/wechat-login"
      wx.request({
        url: url,
        method: "post",
        data: {
          code: res.code
        },
        success: res => {
          if (res.data.message == "success") {
            wx.setStorage({
              key: 'OPEN_ID',
              data: res.data.data.oa_openid.substr(3, res.data.data.oa_openid.length - 2),
            })
          } else {
            // info=res.data.message
            // alert("用户名密码有误")
          }
        }
      })
    }

  })
  // return info
}
/**
 * 根据
 */
const getUser = function () {
  // const app = getApp()
  var url = getApp().globalData.url
  const _TOKEN = wx.getStorageSync('_TOKEN')
  if (_TOKEN != "") {
    wx.request({
      url: url + '/oa-applet/user-info',
      header: {
        token: _TOKEN
      },
      data: {
        user_name: 'true',
        department_name: 'true',
        station_name: 'true',
        user_phone: 'true'
      },
      success: res => {
        /**
         * 获取用户信息
         */
        //  console.log(res.data.data)
        if (res.data.code == 0) {
          // return res.data.data
          // app.globalData.user=res.data.data
          wx.setStorage({
            data: res.data.data,
            key: 'userInfo',
          })
        }
      },
      fail(res) {
        // console.log(res)
      }
    })
    return true
  } else {
    return false
  }
}

function yearArr() {
  var year = []
  var now = new Date().getFullYear()
  for (var i = 0; i <= 5; i++) {
    year[i] = now - i + "年"
  }
  return year;
}

function monArr() {
  var mon = []
  // var now=new Date().getFullYear()
  for (var i = 0; i < 12; i++) {
    
      mon[i] =  (i + 1) + "月"
      mon[i]= mon[i].toString().padStart(3,"0");
  }
  return mon;
}

function json2Form(json) {
  var str = [];
  for (var p in json) {
    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));
  }
  // console.log(str.join("&"))
  return str.join("&");
}
/**
 * 判断该日期是否在工作日
 * @param {int} Y 
 * @param {int} M 
 * @param {int} D 
 */
const ins = function (Y, M, D) {
  var w = new Date(Y + "-" + M + "-" + D).getDay()
  var s = false
  if (w == 1 || w == 2 || w == 3 || w == 4 || w == 5) {
    s = true
  }
  return s
}
/**
 * 对比两个日期
 * @param {*} a 
 * @param {*} b 
 */
const bijiao = function (a, b) {
  a = Number(a)
  b = Number(b)
  return a - b < 0
}
const alert = e => {
  wx.showModal({
    cancelColor: '#666666',
    cancelText: '取消',
    content: e,

  })
}

function testMy() {
  let date = new Date();
  let years = [];
  let months = [];
  let days = [];
  let hours = [];
  let minutes = [];

  for (let i = date.getFullYear(); i <= (date.getFullYear() + 5); i++) {
    years.push(i + "年")
  }
  for (let i = 1; i <= 12; i++) {
    months.push(i + "月")
    months[i-1]=months[i-1].toString().padStart(3,"0")
  }
  for (let i = 1; i <= 31; i++) {
    days.push(i + "日")
    days[i-1]=days[i-1].toString().padStart(3,"0")
  }
  for (let i = 0; i <= 23; i++) {
    i < 10 ? hours.push("0" + i + "") : hours.push(i + "")
  }
  for (let i = 0; i <= 59; i++) {
    i < 10 ? minutes.push("0" + i + "") : minutes.push(i + "")
  }
  return {
    years,
    months,
    days,
    hours,
    minutes
  }
}

function alerts(e, suc) {
  wx.showModal({
    cancelColor: '#616161',
    cancelText: '取消',
    content: e,
    showCancel: true,
    success: suc,
    confirmColor: "#0081DF"
  })
}

function request(url, method, data, header, fun) {
  if (method != 'post' && method != "POST") {
    wx.showLoading({
      title: '数据加载中',
      icon: "loading"
    })
  }
  const http = getApp().globalData.url
  // console.log(http+url)
  // console.log(data)
  wx.request({
    url: http + url,
    method: method,
    data: data,
    header: header,
    success: fun,
    fail: (data) => {
      wx.hideLoading()
      wx.showModal({
        content: "请求超时...",
        showCancel: false,
        success: (res) => {
          // debugger
          this.request(url, method, data, header, fun)
        }
      })
    }
  })
}
const getData = (url, method, data, header) => {
  const http = getApp().globalData.url
  return new Promise((resolve, reject) => {
    wx.request({
      url: http + url,
      method: method,
      data: data,
      header: header,
      success(res) {
        resolve(res.data)
      },
      fail(err) {
        reject(err)
      }
    })
  })
}
var dates=require("../utils/util")
function info(date,user_name,text){
  return dates.formatTime(date)+"： "+user_name+"   "+text
}
module.exports = {
  alert,
  logins: logins,
  getUser: getUser,
  in: ins,
  bijiao: bijiao,
  json2Form: json2Form,
  yearArr,
  monArr,
  request,
  alerts,
  getData,
  testMy,
  info
}